/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jlxy.ise.eroad.service;

import java.util.List;
import java.util.Map;
import jlxy.ise.eroad.controller.Bean.OrderBean;
import jlxy.ise.eroad.controller.Bean.UserOrderBean;
import jlxy.ise.eroad.service.function.ClientFunc;
import jlxy.ise.eroad.util.SqlOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author hcc25_000
 */
@Service
@Transactional
public class PlatformService {

    @Autowired
    private SqlOperator sql;

    //用户输入并且提交货物信息，平台生成该货物的编号，并且把该货物分配给随机的公司(username)，返回对应的cdkey
    public Sret setPackageInfo(UserOrderBean uob) {
        Sret sr = new Sret();
        /*String gaddress = ob.getGaddress();
        String paddress = ob.getPaddress();
        String phonenum = ob.getPhonenum();

        String username = getComInfo();
        if (username.equals("-1")) {
            sr.setFail();
        } else {
            sr.setOk(username);
            phonenum=username;
            int weight = ob.getWeight();
            String cdkey = ClientFunc.getCDKey1();//获取随机的订单号吗
            String gclient = ob.getGclient();
            String pclient = ob.getPclient();
            String car = ob.getCar();
            String time = ClientFunc.getTime();//获取下单的时间
            String stmt = "insert into pack_info2(gaddress,paddress,phonenum,weight,cdkey,gclient,pclient,car,state,time) values(?,?,?,?,?,?,?,?,?,?)";
            this.sql.insert(stmt, gaddress, paddress, phonenum, weight, cdkey, gclient, pclient, car, "waiting", time);
            sr.setData(cdkey);
        }*/
        String comname=getComInfo();
        if(comname.equals("-1")){
            sr.setFail();
        }else{
            sr.setOk();
            String customer=uob.getCustomer();
            String customer_phone=uob.getCustomer_phone();
            String customer_province=uob.getCustomer_province();
            String customer_city=uob.getCustomer_city();
            String customer_district=uob.getCustomer_district();
            String customer_detailed_address=uob.getCustomer_detailed_address();
            
            String receiver=uob.getReceiver();
            String receiver_phone=uob.getReceiver_phone();
            String receiver_province=uob.getReceiver_province();
            String receiver_city=uob.getReceiver_city();
            String receiver_district=uob.getReceiver_district();
            String receiver_detailed_address=uob.getReceiver_detailed_address();
            String state="waiting";
            String cdkey=ClientFunc.getCDKey1();
            String time=ClientFunc.getTime();
            String stmt="insert into user_order(customer,customer_phone,origin_province,origin_city,origin_district,origin_detailed_address,receiver,receiver_phone,receiver_province,receiver_city,receiver_district,receiver_detailed_address,state,cdkey,comname,time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            this.sql.insert(stmt,customer,customer_phone,customer_province,customer_city,customer_district,customer_detailed_address,receiver,receiver_phone,receiver_province,receiver_city,receiver_district,receiver_detailed_address,state,cdkey,comname,time);
            sr.setOk(comname);
            sr.setData(cdkey);
        }
        return sr;
    }

    public String getComInfo() {//返回随机选取的公司username
        Object name = null;
        String stmt = "select username from com_register";
        List list = this.sql.query(stmt);
        //当没有公司用户注册时，cdkey放回-1
        if (list.isEmpty()) {
            name = "-1";
        } else {
            int random = (int) (Math.random() * list.size());
            name = ((Map)list.get(random)).get("username");
        }
        return (String)name;
    }
}
